package com.ruoyi.system.domain.temple;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.NumberFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import lombok.Data;
import lombok.ToString;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 明细对象 procurement_detail
 *
 * @author xuliang
 * @date 2024-03-29
 */
@Data
@ToString
public class DirectTemple
{
    private static final long serialVersionUID = 1L;

    @ExcelIgnore
    private Long id;

    @ExcelProperty(value = "序号")
    @ColumnWidth(8)
    private Long no = 1L;

    /** 采购项目名称 */
    @ExcelProperty(value = "专业分包名称")
    @Excel(name = "专业分包名称")
    @ColumnWidth(15)
    private String directName = "模板样式";

    /** 项目编号 */
    //@ExcelProperty(value = "项目编号")
    //@Excel(name = "项目编号")
    //@ColumnWidth(15)
    @ExcelIgnore
    private String projectCode = "cccc2024000";

    /** 合同额（含税） */
    @ExcelProperty(value = "合同额（含税）")
    @Excel(name = "合同额（含税）")
    @ColumnWidth(20)
    private BigDecimal contractWithTax = new BigDecimal(1000000);

    /** 合同编号 */
    @ExcelProperty(value = "合同编号")
    @Excel(name = "合同编号")
    @ColumnWidth(15)
    private String contractCode = "cccc20240000ht001";

    /** 供应商 */
    @ExcelProperty(value = "供应商")
    @Excel(name = "供应商")
    @ColumnWidth(15)
    private String supplier = "供应商字符串";

    /** 实际进项税率 */
    @ExcelProperty(value = "实际进项税率")
    @Excel(name = "实际进项税率")
    @ColumnWidth(10)
    @NumberFormat(value = "0.00%")
    private Double taxRate = 0.10;

    /** 累计已发生的成本（含税） */
    @ExcelProperty(value = "累计已发生成本（含税）")
    @Excel(name = "累计已发生成本（含税）")
    @ColumnWidth(20)
    private BigDecimal realTotalWithTax = new BigDecimal("1000000.00");

    /** 已发生成本理论进项税额 */
    @ExcelIgnore
    private BigDecimal tax = new BigDecimal("90909.09");

    /** 进度 */
    @ExcelIgnore
    private Double progress = 1.00;

    /** 预计总成本理论进项税额 */
    @ExcelIgnore
    private BigDecimal expectTax = new BigDecimal("90909.09");

    /** 备注 */
    @ExcelProperty(value = "备注")
    @Excel(name = "备注")
    @ColumnWidth(20)
    private String remark = "备注字符串";

    /** 施工借方发生额合计（不含税） */
    @ExcelIgnore
    private BigDecimal totalNoTax;

    // 设置 realTotalWithTax 属性值的 setter 方法
    public void setRealTotalWithTax(BigDecimal realTotalWithTax) {
        this.realTotalWithTax = realTotalWithTax;
        calculateTax(); // 在设置属性值后调用计算方法
    }

    // 设置 contractWithTax 属性值的 setter 方法
    public void setContractWithTax(BigDecimal contractWithTax) {
        this.contractWithTax = contractWithTax;
        calculateTax(); // 在设置属性值后调用计算方法
    }

    // 计算
    public void calculateTax() {
        if ((realTotalWithTax != null && !(BigDecimal.ZERO.equals(realTotalWithTax))) && taxRate != null) {
            BigDecimal divisor = BigDecimal.ONE.add(BigDecimal.valueOf(taxRate));
            tax = realTotalWithTax.divide(divisor, 2, RoundingMode.HALF_UP)
                    .multiply(BigDecimal.valueOf(taxRate));
        } else {
            tax = BigDecimal.ZERO;
        }

        if((contractWithTax != null && !(BigDecimal.ZERO.equals(contractWithTax))) && taxRate != null){
            BigDecimal divisor = BigDecimal.ONE.add(BigDecimal.valueOf(taxRate));
            expectTax = contractWithTax.divide(divisor, 2, RoundingMode.HALF_UP)
                    .multiply(BigDecimal.valueOf(taxRate));
        } else {
            expectTax = BigDecimal.ZERO;
        }

        if(realTotalWithTax != null && contractWithTax != null
                && contractWithTax.compareTo(BigDecimal.ZERO) != 0){
            progress = Double.valueOf(realTotalWithTax.divide(contractWithTax, 2, RoundingMode.HALF_UP).toString());
        }else {
            progress = 0.0;
        }
    }
}
